package gui;

import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.m3g.Graphics3D;
import javax.microedition.m3g.Loader;
import javax.microedition.m3g.Object3D;
import javax.microedition.m3g.World;

import logic.BtClientConnection;
import logic.LogicHelper;
import logic.MainMidlet;
import logic.BtServerConnection;

/**
 * screen for waiting for clients connections or for server (depending on role - client or server)
 * @author Rekin
 *
 */
public class WaitingScreen extends Canvas implements CommandListener{

	private BtServerConnection scs;
	private BtClientConnection ccs;
	
	private String screenTitle;
	private String screenInfo;
	//scenery variables
	private World world;
	private Graphics3D g3d;
	Timer iTimer;
	private int time;
	private boolean graphicsReady=true;
	
	/**
	 * constructor
	 * @param type type of screen (client or server)
	 * @param title title of screen
	 * @param info information for user
	 */
	public WaitingScreen(int type, String title, String info){
		screenTitle=title;
		screenInfo=info;
		scs=null;
		ccs=null;
		
		initBluetoothSearch(type);
		initWorld();
		addCommand(new Command("Cancel", Command.EXIT, 2));
		setCommandListener(this);
	}
	
	/**
	 * initiates bluetooth search
	 * @param type type of waiting screen and created connection (server or client)
	 */
	private void initBluetoothSearch(int type){
		iTimer = new Timer();
		time=0;
		switch(type){
		case LogicHelper.clientWaitingScreen:
			ccs=new BtClientConnection();
			break;
		case LogicHelper.serverWaitingScreen:
			scs=new BtServerConnection();
			break;
		}
	}
	
	/**
	 * initiates screen graphics
	 */
	private void initWorld(){
		g3d = Graphics3D.getInstance();
		try{
			Object3D[] roots = Loader.load("/loading.m3g");
			world = (World)roots[0];
			iTimer.schedule(new TimerTask(){
				public void run(){
					repaint();
				} }, 0, 5);
		}
		catch(IOException ex){
			graphicsReady=false;
			System.out.println("Error scene load");
		}
	}
	
	/**
	 * painting elements
	 */
	protected void paint(Graphics g) {
		if(graphicsReady){
			time+=50;
			world.animate(time%6250);
			g3d.bindTarget(g);
			g3d.render(world);
			g3d.releaseTarget();
		}
		g.setColor(0, 255, 255);
		g.drawString(screenTitle, 0, 0, Graphics.TOP | Graphics.LEFT);
		g.drawString(screenInfo, getWidth()/2 , 30, Graphics.HCENTER | Graphics.TOP);
	}
	
	/**
	 * inner timer class for refreshing screen
	 * @author Rekin
	 *
	 */
	class MyTimerTask extends TimerTask{
		public void run() {
			repaint();
		}
	}

	/**
	 * handling user actions
	 */
	public void commandAction(Command c, Displayable d) {
		if(c.getCommandType() == Command.EXIT){
			if(ccs != null){
				ccs.stopBt(true);
			}
			else if(scs != null){
				scs.stopListening();
			}
			iTimer.cancel();
			MainMidlet.getMidletInstance().showMainMenu();
		}
	}

}
